//
//  TSClipMaskImageAdjustGRViewController.m
//  TSDemo_ImageFilter
//
//  Created by qian on 2021/3/5.
//

#import "TSClipMaskImageAdjustGRViewController.h"
#import <Masonry/Masonry.h>
#import <CQDemoKit/CQTSContainerViewFactory.h>
#import <CQDemoKit/CQTSLocImagesUtil.h>
#import <CQGRKit/CJMaskImageAdjustGRView.h>
#import "BLClipImageView.h"

@interface TSClipMaskImageAdjustGRViewController () {
    
}
@property (nonatomic, strong) CJMaskImageAdjustGRView *imageScaleView;

@end

@implementation TSClipMaskImageAdjustGRViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIView *clipButtonsView = [CQTSContainerViewFactory threeButtonsViewAlongAxis:MASAxisTypeHorizontal title1:@"矩形裁剪" actionBlock1:^(UIButton * _Nonnull bButton) {
        UIImage *clipResultImage = [self getSmallImage:CJRectPathTypeRectangle];
        [self.imageScaleView imageView].image = clipResultImage;
        [self.imageScaleView updateFrameByImage:clipResultImage];
        
    } title2:@"圆形裁剪" actionBlock2:^(UIButton * _Nonnull bButton) {
        UIImage *clipResultImage = [self getSmallImage:CJRectPathTypeCircle];
        [self.imageScaleView imageView].image = clipResultImage;
        [self.imageScaleView updateFrameByImage:clipResultImage];
        
    } title3:@"未确定" actionBlock3:^(UIButton * _Nonnull bButton) {
        
    }];
    [self.view addSubview:clipButtonsView];
    [clipButtonsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.mas_bottomLayoutGuide).mas_offset(-20);
        make.height.mas_equalTo(44);
        make.centerX.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view).mas_offset(20);
    }];
    
    UIView *buttonsView = [CQTSContainerViewFactory threeButtonsViewAlongAxis:MASAxisTypeVertical title1:@"竖直长图" actionBlock1:^(UIButton * _Nonnull bButton) {
        UIImage *image = [CQTSLocImagesUtil longVertical01];
        [self.imageScaleView imageView].image = image;
        [self.imageScaleView updateFrameByImage:image];
        
    } title2:@"水平宽图" actionBlock2:^(UIButton * _Nonnull bButton) {
        UIImage *image = [CQTSLocImagesUtil longHorizontal01];
        [self.imageScaleView imageView].image = image;
        [self.imageScaleView updateFrameByImage:image];
        
    } title3:@"随机图" actionBlock3:^(UIButton * _Nonnull bButton) {
        UIImage *image = [CQTSLocImagesUtil cjts_localImageRandom];
        [self.imageScaleView imageView].image = image;
        [self.imageScaleView updateFrameByImage:image];
        
    }];
    [self.view addSubview:buttonsView];
    [buttonsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(clipButtonsView.mas_top).mas_offset(-20);
        make.height.mas_equalTo(50*2+10*1);
        make.centerX.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view).mas_offset(20);
    }];
    
    BLClipImageView *imageScaleView = [[BLClipImageView alloc] initWithFrame:CGRectZero];
    imageScaleView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:imageScaleView];
    [imageScaleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.left.mas_equalTo(self.view).mas_offset(20);
        make.top.mas_equalTo(self.mas_topLayoutGuide).mas_offset(20);
        make.bottom.mas_equalTo(buttonsView.mas_top).mas_offset(-20);
    }];
    self.imageScaleView = imageScaleView;
    imageScaleView.pinchMaxScale =  2;
    
    
    UIImage *image = [CQTSLocImagesUtil cjts_localImageRandom];
    [imageScaleView imageView].image = image;
    [imageScaleView updateFrameByImage:image];
}


//方形裁剪
- (UIImage *)getSmallImage:(CJRectPathType)pathType
{
    UIImage *image = self.imageScaleView.imageView.image;
    CGRect clippingPixelRect = [self.imageScaleView getClippingPixelRect];

    CGImageRef  imageRef = CGImageCreateWithImageInRect(image.CGImage, clippingPixelRect);
    UIGraphicsBeginImageContext(clippingPixelRect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, clippingPixelRect, imageRef);
    UIImage * clipImage = [UIImage imageWithCGImage:imageRef];
    UIGraphicsEndImageContext();
    
    if(pathType == CJRectPathTypeCircle)
        return  [self CircularClipImage:clipImage];
    
    return clipImage;
}

//圆形图片
-(UIImage *)CircularClipImage:(UIImage *)image
{
    CGFloat arcCenterX = image.size.width/ 2;
    CGFloat arcCenterY = image.size.height / 2;
    
    UIGraphicsBeginImageContext(image.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextBeginPath(context);
    CGContextAddArc(context, arcCenterX , arcCenterY, image.size.width/ 2 , 0.0, 2*M_PI, NO);
    CGContextClip(context);
    CGRect myRect = CGRectMake(0 , 0, image.size.width ,  image.size.height);
    [image drawInRect:myRect];
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return  newImage;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
